<template>
  <div class="container">
    <div class="section-title">
      <span>热门报告</span>
    </div>
    <div class="product-list">
      <div
        class="product-item"
        v-for="(item, index) in reportList"
        :key="index"
        @click="handleRowClick(item)"
      >
        <div class="hover-mask">
          <div class="hover-buttons">
            <el-button
              class="btn-view"
              @click.stop.prevent="handleView(item)"
              :icon="View"
            >
              查看详情
            </el-button>
            <el-button
              class="btn-download"
              @click.stop.prevent="handleDownload(item)"
              :icon="Download"
            >
              下载报告
            </el-button>
          </div>
        </div>

        <div class="product-item-right">
          <p class="title">{{ item.title }}</p>
          <p class="introduce">{{ item.script }}</p>
          <p class="meta">
            <span class="meta-item">
              <el-icon><User /></el-icon>
              {{ item.author }}
            </span>
            <span class="meta-item">
              <el-icon><Calendar /></el-icon>
              {{ dayjs.unix(item.publishTime).format("YYYY-MM-DD") }}
            </span>
            <span v-if="item.page" class="meta-item">
              <el-icon><Document /></el-icon>
              {{ item.sizeNum }}
            </span>
          </p>
          <div class="action-area">
            <el-tag type="info">{{ item.PM_industry_1_name }}</el-tag>
            <el-tag :type="item.price2 === '免费' ? 'primary' : 'warning'">
              ￥{{ item.price }}
            </el-tag>
          </div>
        </div>

        <div class="img-wrapper">
          <img :src="item.coverPic" alt="" />
          <div class="img-title">{{ item.title }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import dayjs from 'dayjs'
import { useRouter } from 'vue-router'
import { reportApi } from '../../api/report.js'
import { User, Calendar, Document, View, Download } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const router = useRouter()
const reportList = ref([])

const handleView = (item) => {
  // 检查是否为免费报告
if (item.price === '0') {
    // 解析免费报告详情页的完整URL
    const freeUrl = router.resolve({
      name: 'productDetail',
      params: { code: item.code }
    }).href;
    
    // 在新标签页打开免费报告
    window.open(freeUrl, '_blank');
  } else {
    // 解析付费报告详情页的完整URL
    const paidUrl = router.resolve({
      name: 'productDetailcharge',
      params: { code: item.code }
    }).href;
    
    // 在新标签页打开付费报告
    window.open(paidUrl, '_blank');
  }
}

const handleDownload = async (item) => {
  try {
    const res = await reportApi.reportDownload({ code: item.code })
    if (res.code === 0) {
      const a = document.createElement('a')
      a.href = res.data.userDown.pdfDir
      a.download = ''
      document.body.appendChild(a)
      a.click()
      document.body.removeChild(a)
      ElMessage.success('下载开始')
    } else {
      ElMessage.error(res.msg || '下载失败')
    }
  } catch {
    ElMessage.error('下载出错')
  }
}

onMounted(() => {
  reportApi.getSumData().then(res => {
    reportList.value = res.data.listHot
  })
})
</script>

<style lang="scss" scoped>
.container {
  width: 80%;
  margin: 40px auto;
  display: flex;
  flex-direction: column;

  .section-title {
    position: relative;
    display: inline-block;
    font-size: 24px;
    color: #333;
    text-align: center;
    margin: 20px auto;

    &::before {
      content: "Popular Reports";
      position: absolute;
      top: -25px;
      left: 50%;
      transform: translateX(-50%);
      font-size: 50px;
      color: rgba(0, 0, 0, 0.1);
      white-space: nowrap;
      font-weight: 800;
    }
    &::after {
      content: "";
      display: block;
      width: 60px;
      height: 3px;
      background-color: #0080d1;
      margin: 8px auto 0;
    }
    span {
      font-weight: 600;
      font-size: 40px;
      line-height: 100%;
    }
  }
}

.product-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 24px;
}

.product-item {
  position: relative;
  display: flex;
  align-items: flex-start;
  width: calc(50% - 12px);
  height: 260px;
  background: linear-gradient(to right, #fff, #e5f1f9);
  border-radius: 12px;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  cursor: pointer;
  transition: all 0.3s ease;

  &:hover .hover-mask {
    opacity: 1;
    .hover-buttons {
      transform: translateY(30px);
    }
  }

  .hover-mask {
    position: absolute;
    inset: 0;
    background: #1e6a9a3d;
    opacity: 0;
    z-index: 2;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s ease;

    .hover-buttons {
      display: flex;
      /* 初始状态下按钮下移更多一点 */
      transform: translateY(30px);
      transition: transform 0.3s ease;
    }
  }
  .btn-view {
    background: #fff !important;
    color: #0080d1 !important;
    width: 140px;
  }
  .btn-download {
    background: #0080d1 !important;
    color: #fff !important;
    width: 140px;
  }

  .product-item-right {
    flex: 1;
    padding: 15px;
    margin-top: 0;

    .title {
      color: #333;
      font-weight: 600;
      font-size: 20px;
    }
    .introduce {
      font-size: 14px;
      font-weight: 600;
      color: #999;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
      overflow: hidden;
    }
    .meta {
      display: flex;
      gap: 10px;
      margin-top: 12px;
      align-items: center;
      color: #999;

      &-item {
        display: flex;
        align-items: center;
        gap: 6px;
        font-size: 14px;

        .el-icon {
          color: #151618b8;
          font-size: 16px;
          margin-right: 4px;
        }
      }
    }
    .action-area {
      display: flex;
      align-items: center;
      margin-top: 12px;

      .el-tag {
        margin-right: 10px;
      }
    }
  }

  .img-wrapper {
    flex: 0 0 194px;
    position: relative;
    height: 260px;

    img {
      display: block;
      margin: 40px auto 0;
      width: 130px;
      height: 191px;
      object-fit: cover;
      border-bottom: 1px solid #eee;
    }
    .img-title {
      position: absolute;
      top: 30%;
      left: 50%;
      transform: translate(-50%, -50%) rotate(-5deg);
      font-size: 12px;
      font-weight: bold;
      font-style: italic;
      color: #fff;
      text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
      white-space: nowrap;
      width: 100px;
      overflow: hidden;
      text-overflow: ellipsis;
      pointer-events: none;
    }
  }
}
</style>
